package com.test.clyq.daoImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.dao.DataAccessException;

import com.test.clyq.dao.UsersLogDao;
import com.test.clyq.entity.Users;
import com.test.clyq.entity.UsersLog;
import com.test.dao.hibernate.BaseDaoHibernate;
import com.test.util.FormatExceptionStack;
import com.test.util.Pagination;
import com.test.util.SessionFactoryUtil;

/**
 * UsersLogDaoHibernate. <br>
 * 管理员日志信息.
 * <p>
 * Copyright: Copyright (c) 2010-4-17 下午04:38:58
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
@SuppressWarnings("unchecked")
public class UsersLogDaoHibernate extends BaseDaoHibernate implements UsersLogDao {

	/**
	 * 新增用户日志信息
	 * 
	 * @param userLog
	 *            用户日志实例
	 */
	public void addUsersLog(UsersLog userLog) {
		this.log.info("新增用户日志信息start");
		try {
			getHibernateTemplate().save(userLog);
			// saveOrUpdateItem(user,null);
			this.log.debug("新增用户日志信息成功");
			this.log.info("新增用户日志信息end");
		}
		catch (DataAccessException e) {
			this.log.error("[UsersLog]addUsersLog发生错误！\n" + FormatExceptionStack.formatException(e));
		}

	}

	/**
	 * 根据管理员日志ID删除管理员日志
	 * 
	 * @param id
	 *            管理员日志ID
	 * @param user
	 *            用户实例
	 * @return true or false
	 */
	public boolean delUsersLogById(Long id, Users user) {
		this.log.info("根据管理员日志ID删除管理员日志start");
		try {
			UsersLog usersLog = (UsersLog) getItem(UsersLog.class, id);
			if (usersLog != null) {
				removeItem(UsersLog.class, id, user);
				this.log.debug("删除管理员日志信息成功");
				this.log.info("根据管理员日志ID删除管理员日志end");
				return true;
			} else {
				this.log.debug("删除管理员日志信息失败");
				return false;
			}
		}
		catch (DataAccessException e) {
			this.log.error("[UsersLog]delNLUsersLogById发生错误！\n" + FormatExceptionStack.formatException(e));
			return false;
		}
	}

	/**
	 * 获得管理员日志信息列表
	 * 
	 * @return 管理员日志信息列表结果集合
	 */
	public List<UsersLog> getUsersLog() {
		this.log.info("获得管理员日志信息列表start");

		try {
			List list = getItems(UsersLog.class);
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("获得管理员日志信息列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return new ArrayList<UsersLog>();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[UsersLog]getUsersLog发生错误！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<UsersLog>();
		}
	}

	/**
	 * 根据管理员日志id获得管理员日志信息
	 * 
	 * @param id
	 *            管理员日志ID
	 * @return UsersLog 管理员日志实体
	 */
	public UsersLog getUsersLogById(Long id) {
		this.log.info("根据管理员日志ID获得管理员日志信息start");
		try {
			UsersLog usersLog = (UsersLog) getItem(UsersLog.class, id);
			if (usersLog != null) {
				this.log.info("根据管理员日志ID获得管理员日志信息end");
				return usersLog;
			} else {
				this.log.info("根据管理员日志ID获得管理员日志信息end");
				return new UsersLog();
			}

		}
		catch (DataAccessException e) {
			this.log.error("[UsersLog]getNLUsersLogById发生错误！\n" + FormatExceptionStack.formatException(e));
			return new UsersLog();
		}
	}

	/**
	 * 根据用户登陆名获得管理员日志信息
	 * 
	 * @param name
	 *            用户登陆名
	 * @return UsersLog 管理员日志实例
	 */
	public UsersLog getUsersLogByName(String name) {
		this.log.info("根据用户登陆名获得管理员日志信息start");
		String sql = "from UsersLog as nluserslog where nluserslog.usersLoginName=:name ";
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery(sql.toString());
			query.setString("name", name);

			List list = query.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("根据用户登陆名获得管理员日志信息end");
				UsersLog usersLog = (UsersLog) list.get(0);
				return usersLog;
			} else {
				this.log.debug("表中没有数据");
				return new UsersLog();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[UsersLog]getUsersLogByName发生错误！\n" + FormatExceptionStack.formatException(e));
			return new UsersLog();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}
	}

	/**
	 * 根据用户ID获得管理员日志信息
	 * 
	 * @param usersId
	 *            用户ID
	 * @return UsersLog 管理员日志实例
	 */
	public UsersLog getUsersLogByUsersId(Long usersId) {
		this.log.info("根据用户id获得管理员日志信息start");
		String sql = "from UsersLog as nluserslog where nluserslog.usersId=:usersid ";
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery(sql.toString());
			query.setLong("usersid", usersId);

			List list = query.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("根据用户id获得管理员日志信息end");
				UsersLog usersLog = (UsersLog) list.get(0);
				return usersLog;
			} else {
				this.log.debug("表中没有数据");
				return new UsersLog();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[UsersLog]getUsersLogByUsersId发生错误！\n" + FormatExceptionStack.formatException(e));
			return new UsersLog();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}
	}

	/**
	 * 获得管理员日志列表（分页使用）
	 * 
	 * @return 管理员日志信息列表结果集合
	 */
	public List<UsersLog> usersLogList(Pagination pagination) {
		this.log.info("获得管理员日志信息列表start");
		String sql = "from UsersLog as nluserslog order by nluserslog.id  desc";
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery("select count(*) " + sql);
			List listCount = query.list();
			if (listCount != null && !listCount.isEmpty()) {
				pagination.setTotalRows(((Integer) listCount.get(0)).intValue()); // 获得记录总数
			} else {
				pagination.setTotalRows(0);
			}

			Query query1 = session.createQuery(sql.toString());
			query1.setFirstResult(pagination.getStartIndex());
			query1.setMaxResults(pagination.getNumPerPage());
			List list = query1.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("获得管理员日志信息列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return new ArrayList<UsersLog>();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[UsersLog]userList发生错误！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<UsersLog>();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}
	}

	/**
	 * 获得管理员日志列表（搜索使用）
	 * 
	 * @return 管理员日志信息列表结果集合
	 */
	public List<UsersLog> usersLogListBySearch(Long userId, Long starTime, Long endTime, Pagination pagination) {
		this.log.info("获得管理员日志信息列表start");
		String sql = "from UsersLog as nluserslog order by nluserslog.id  desc";
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		if (userId != -1) {

			if (starTime != -1 && endTime != -1) {
				sql = "from UsersLog as nluserslog where nluserslog.usersId=:uId and nluserslog.operateTime  between :sTime and :eTime order by nluserslog.id  desc";
			} else if (starTime != -1) {
				sql = "from UsersLog as nluserslog where nluserslog.usersId=:uId and nluserslog.operateTime > :sTime  order by nluserslog.id  desc";
			} else {
				sql = "from UsersLog as nluserslog where nluserslog.usersId=:uId  order by nluserslog.id  desc";
			}
		} else if (starTime != -1 && endTime != -1) {
			sql = "from UsersLog as nluserslog where nluserslog.operateTime  between :sTime and :eTime order by nluserslog.id  desc";
		} else if (starTime != -1) {
			sql = "from UsersLog as nluserslog where nluserslog.operateTime > :sTime  order by nluserslog.id  desc";
		}

		try {
			Query query = session.createQuery("select count(*) " + sql);
			if (userId != -1) {

				if (starTime != -1 && endTime != -1) {
					query.setLong("sTime", starTime);
					query.setLong("uId", userId);
					query.setLong("eTime", endTime);
				} else if (starTime != -1) {
					query.setLong("uId", userId);
					query.setLong("sTime", starTime);
				} else {
					query.setLong("uId", userId);
				}
			} else if (starTime != -1 && endTime != -1) {
				query.setLong("sTime", starTime);
				query.setLong("eTime", endTime);

			} else if (starTime != -1) {
				query.setLong("sTime", starTime);
			}
			List listCount = query.list();
			if (listCount != null && !listCount.isEmpty()) {
				pagination.setTotalRows(((Integer) listCount.get(0)).intValue()); // 获得记录总数
			} else {
				pagination.setTotalRows(0);
			}

			Query query1 = session.createQuery(sql.toString());

			if (userId != -1) {

				if (starTime != -1 && endTime != -1) {
					query1.setLong("sTime", starTime);
					query1.setLong("uId", userId);
					query1.setLong("eTime", endTime);
				} else if (starTime != -1) {
					query1.setLong("uId", userId);
					query1.setLong("sTime", starTime);
				} else {
					query1.setLong("uId", userId);
				}
			} else if (starTime != -1 && endTime != -1) {
				query1.setLong("sTime", starTime);
				query1.setLong("eTime", endTime);

			} else if (starTime != -1) {
				query1.setLong("sTime", starTime);
			}
			query1.setFirstResult(pagination.getStartIndex());
			query1.setMaxResults(pagination.getNumPerPage());
			List list = query1.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("获得管理员日志信息列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return new ArrayList<UsersLog>();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[UsersLog]userList发生错误！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<UsersLog>();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}
	}

}
